package com.shigu.cron;

import com.opentae.data.mall.beans.UserTask;
import com.opentae.data.mall.examples.UserTaskExample;
import com.opentae.data.mall.interfaces.UserTaskMapper;
import com.shigu.task.service.MallTaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @author 郭尔
 * @description 定时给完成任务的用户打钱
 * @date 2019-09-20 15:23
 */

@Component
public class CompleteTask {
    @Autowired
    UserTaskMapper userTaskMapper;
    @Resource(name = "mallTaskServiceImpl")
    MallTaskService mallTaskService;


    @Scheduled(cron="0/5 * * * * ?")
    public void modifyFlashNum(){

        Date now = new Date();
        long time = now.getTime() - (1000 * 60 * 60 * 48);
        Date ago = new Date(time);
        //查出所有2天前提交的未审核的任务
        UserTaskExample example = new UserTaskExample();
        example.createCriteria().andStatusEqualTo(1).andSubmitTimeLessThanOrEqualTo(ago);
        List<UserTask> tasks = userTaskMapper.selectByExample(example);
        for (UserTask task: tasks) {
            mallTaskService.completeTask(task.getCatchId());
        }
    }
}
